package pack;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Date;

import aspectJ.Logg;

public class Transeunte extends Thread {

	private Socket s = null;
	private BufferedReader request = null;
	private DataOutputStream response = null;
	StringBuilder r;
	String mac = "";
	Date inicioConstrutor;
	Date inicioRun;
	Date fimRun;
	Logg log;

	// @ requires s != null;
	public Transeunte(Socket s) {
		inicioConstrutor = new Date(System.currentTimeMillis());
		setS(s);
		this.start();
	}

	@Override
	public void run() {
		inicioRun = new Date(System.currentTimeMillis());
		try {
			request = new BufferedReader(new InputStreamReader(
					this.s.getInputStream()));

			response = new DataOutputStream(this.s.getOutputStream());

			r = new StringBuilder();
			String tmp;

			while (!(tmp = request.readLine()).equals("")) {
				r.append(tmp + "\n");
				System.out.println(tmp);
			}

			mac = Interpreter.understand(r);
			response.writeBytes(mac);

			this.s.close();

		} catch (IOException e) {
			System.err.println(e.getMessage() + "\n" + e.getStackTrace());
		} finally {
			fimRun = new Date(System.currentTimeMillis());
		}
	}

	public Socket getS() {
		return s;
	}

	public void setS(Socket s) {
		this.s = s;
	}

	public BufferedReader getRequest() {
		return request;
	}

	public void setRequest(BufferedReader request) {
		this.request = request;
	}

	public DataOutputStream getResponse() {
		return response;
	}

	public void setResponse(DataOutputStream response) {
		this.response = response;
	}

	public StringBuilder getR() {
		return r;
	}

	public void setR(StringBuilder r) {
		this.r = r;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public Date getInicioConstrutor() {
		return inicioConstrutor;
	}

	public void setInicioConstrutor(Date inicioConstrutor) {
		this.inicioConstrutor = inicioConstrutor;
	}

	public Date getInicioRun() {
		return inicioRun;
	}

	public void setInicioRun(Date inicioRun) {
		this.inicioRun = inicioRun;
	}

	public Date getFimRun() {
		return fimRun;
	}

	public void setFimRun(Date fimRun) {
		this.fimRun = fimRun;
	}
}
